package com.wan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wan.common.R;
import com.wan.pojo.Employee;
import com.wan.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        // 1.把收到的密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        // 2.根据用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        // 3.判断emp是否为空
        if (emp == null){
            return R.error("登陆失败，用户不存在!");
        }
        // 4.密码比对
        if (!emp.getPassword().equals(password)){
            return R.error("登录失败，密码错误！");
        }

        // 5.状态码比对 员工是否被禁用
        if (emp.getStatus()==0){
            return R.error("账号已被禁用");
        }

        // 6.验证通过为正常用户可以登录 存入id到session
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功！");
    }

    /**
     * 新增员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        // 设置初始化密码 需要使用md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        // 创建时间
//        employee.setCreateTime(LocalDateTime.now());
//        // 最后修改时间
//        employee.setUpdateTime(LocalDateTime.now());
//        // 获取当前登录员工的id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        // 创建员工当前员工id
//        employee.setCreateUser(empId);
        // 最后修改人id
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增用户成功");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        log.info("page:{},pageSize:{},name:{}",page,pageSize,name);

        // 构造分页构造器
        Page pageInfo = new Page(page,pageSize);
        // 构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        // 当name不为空的时候需要添加name条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        // 设置排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        // 调用分页查询语句
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 修改员工信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee){
        // 设置最后修改时间
//        employee.setUpdateTime(LocalDateTime.now());
//        // 设置最后修改人
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("更新成功");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable("id") int id){
        Employee employee = employeeService.getById(id);
        if (employee != null){
            return R.success(employee);
        }
        return R.error("员工信息不存在！");
    }

}
